/**
 * @(#) InrecMediator.as
 *
 * @date		2012. 11. 07.
 * @version		1.0
 * @author		Park, Jutae
 * @description	통신비신청신청 - Mediator
 * 				Inrec를 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.skbcomm.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.skbcomm.SkbcommFacade;
	import com.skt.common_hr.skbcomm.events.ComboBoxYearEvent;
	import com.skt.common_hr.skbcomm.model.SkbCommProxy;
	import com.skt.common_hr.skbcomm.model.vo.SkbVO;
	import com.skt.common_hr.skbcomm.view.components.ReqSkbComm;
	import com.skt.common_hr.skbcomm.view.components.SkbCommView;
	import com.wcl.utils.DateUtil;
	
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class SkbCommMediator extends OverViewMediator
	{
		public static const NAME:String = "InrecMediator"; 


		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function SkbCommMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		
		override protected function initializeView():void
 		{
			super.initializeView();
 		}
		
 		override protected function activatedView():void
 		{
 			setEventListeners();
 			view.setDateRange();
 			loadList();
 		} 
 		
 		override protected function closedView():void
 		{
			removeEventListeners(); 			
 		}
 		
 		private function setEventListeners():void
 		{
 			view.addEventListener(SkbCommView.Serach, loadList);
 			view.addEventListener(ReqSkbComm.Insert, onSinChung);
 			view.addEventListener(SkbCommView.sinChungCheck, onSinChungCheck);
 			view.addEventListener(SkbCommView.billingCall, onBillingCall);
 			view.addEventListener(SkbCommView.Delete, onDelete);
 			view.addEventListener(SkbCommView.End, onEnd);
 			view.addEventListener(SkbCommView.dgClick, onRead);
 			view.addEventListener(ComboBoxYearEvent.SELECT_YEAR_DATA, onComboYear);
 		}
 		
 		private function removeEventListeners():void
 		{
 			view.removeEventListener(SkbCommView.Serach, loadList);
 			view.removeEventListener(ReqSkbComm.Insert, onSinChung);
 			view.removeEventListener(SkbCommView.billingCall, onBillingCall);
 			view.removeEventListener(SkbCommView.sinChungCheck, onSinChungCheck);
 			view.removeEventListener(SkbCommView.Delete, onDelete);
 			view.removeEventListener(SkbCommView.End, onEnd);
 			view.removeEventListener(SkbCommView.dgClick, onRead);
 			view.removeEventListener(ComboBoxYearEvent.SELECT_YEAR_DATA, onComboYear);
 		}
 		
 		/* 통신비신청 클릭시 List 조회 */
 		private function loadList(e:Event=null):void 
 		{
 			view.vs.selectedIndex = 0;
 			
 			var proxy:SkbCommProxy = facade.retrieveProxy(SkbCommProxy.NAME) as SkbCommProxy;
 			proxy.load(Variables.myPernr, view.sDay, view.eDay);
 		}
 		
 		/*  통신비신청 클릭시 */
 		private function onSinChung(e:Event):void
 		{
 			var obj:SkbVO = view.reqView.dataVoList;
			
 			var proxy:SkbCommProxy = facade.retrieveProxy(SkbCommProxy.NAME) as SkbCommProxy;
 			proxy.insert(obj);
 		}
 		
 		
 		/* 통신비신청 신청 가능여부 */
 		private function onSinChungCheck(e:Event):void
 		{
 			var proxy:SkbCommProxy = facade.retrieveProxy(SkbCommProxy.NAME) as SkbCommProxy;
 			proxy.loadCheck(Variables.myPernr);
 			 			
 		}
 		
 		/* 청구내역 팝업 호출 */
 		private function onBillingCall(e:Event):void
 		{
 			//var currentYear:String = e.target.selectedItem.data;
 			var date:Date= DateUtil.todayNoTime();
 			var currentYear:String = date.fullYear.toString();
 			var proxy:SkbCommProxy = facade.retrieveProxy(SkbCommProxy.NAME) as SkbCommProxy;
 			proxy.loadBillingCall(Variables.myPernr, currentYear);
 			 			
 		}
 		
		/* 통신비신청 조회 클릭시 */
		private function onRead(event:Event):void
		{
			var _vo:SkbVO = new SkbVO();
 			if(view.typeStr == "READ"){
 				_vo =  view.dg.selectedItem as SkbVO;
 			}
			
			var proxy:SkbCommProxy = facade.retrieveProxy(SkbCommProxy.NAME) as SkbCommProxy;
 			proxy.readLoad(_vo);
		}
		
		/* 통신비신청 조회 체크 성공시 */
		private function onReadCheck():void
		{
			var _vo:SkbVO = new SkbVO();
			
			var proxy:SkbCommProxy = facade.retrieveProxy(SkbCommProxy.NAME) as SkbCommProxy;
 			proxy.readLoad(_vo);
		}
		
		/* 통신비신청 삭제 클릭시 */
		private function onDelete(event:Event):void
		{
			var obj:SkbVO = view.dg.selectedItem as SkbVO;
			
 			var proxy:SkbCommProxy = facade.retrieveProxy(SkbCommProxy.NAME) as SkbCommProxy;
 			proxy.del(obj); 
			
		}
		
		/* 통신비신청 종료 클릭시 */
		private function onEnd(event:Event):void
		{
			var obj:SkbVO = view.dg.selectedItem as SkbVO;
			
 			var proxy:SkbCommProxy = facade.retrieveProxy(SkbCommProxy.NAME) as SkbCommProxy;
 			proxy.end(obj); 
			
		}
		
		/* 통신비신청 다운로드 클릭시 */
		private function onStatementViewAttach(event:Event):void
 		{
 			var obj:SkbVO = view.dg.selectedItem as SkbVO;
 			
 			var statementProxy:SkbCommProxy = facade.retrieveProxy(SkbCommProxy.NAME) as SkbCommProxy;
			statementProxy.loadAttach(Variables.myPernr, obj);
 		}
 		
 		/* 청구내역 대상년도 변경시 */
 		private function onComboYear(event:ComboBoxYearEvent):void
 		{
 			var cbProxy:SkbCommProxy = facade.retrieveProxy(SkbCommProxy.NAME) as SkbCommProxy;
			cbProxy.loadBillingComboCall(Variables.myPernr, event.comData);
 		}
 		
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat([
					SkbcommFacade.LOAD_LIST_COMPLETE,
					SkbcommFacade.LOAD_LIST_FAILED,
					SkbcommFacade.INSERT_COMPLETE,
					SkbcommFacade.INSERT_FAILED,
					SkbcommFacade.DELETE_COMPLETE,
					SkbcommFacade.DELETE_FAILED,
					SkbcommFacade.READ_LIST_COMPLETE,
					SkbcommFacade.READ_LIST_FAILED,
					SkbcommFacade.LOAD_CHECK_COMPLETE,
					SkbcommFacade.LOAD_CHECK_FAILED,
					SkbcommFacade.OK_REQUEST_INPUT,
					SkbcommFacade.CANCEL_REQUEST_INPUT,
					SkbcommFacade.LOAD_READ_COMPLETE,
					SkbcommFacade.LOAD_READ_FAILED,
					SkbcommFacade.LOAD_READ_COMBO_COMPLETE,
					SkbCommProxy.LOAD_STATEMENT_URL_COMPLTE,
					SkbCommProxy.LOAD_STATEMENT_URL_FAILED
					]);
		}
		
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case SkbcommFacade.LOAD_CHECK_COMPLETE:
					onReadCheck();
					break;
				case SkbcommFacade.LOAD_CHECK_FAILED:
					Alert.show((SkbCommProxy(note.getBody()).failedCheckAlert));
					break;
				case SkbcommFacade.LOAD_LIST_COMPLETE:
					onLoadList(note.getBody() as SkbCommProxy);
					break;
				case SkbcommFacade.LOAD_LIST_FAILED:
					break;
				case SkbcommFacade.LOAD_READ_COMPLETE:
					onLoadBillingList(note.getBody() as SkbCommProxy);
					break;
				case SkbcommFacade.LOAD_READ_COMBO_COMPLETE:
					onLoadComboBillingList(note.getBody() as SkbCommProxy);
					break;	
				case SkbcommFacade.LOAD_READ_FAILED:
					break;	
				case SkbcommFacade.READ_LIST_COMPLETE:
					onReadList(note.getBody() as SkbCommProxy);
					break;
				case SkbcommFacade.READ_LIST_FAILED:
					break;	
				case SkbcommFacade.OK_REQUEST_INPUT:
					view.vs.selectedIndex = 0;
					loadList();
					break;
				case SkbcommFacade.CANCEL_REQUEST_INPUT:
					Alert.show((SkbCommProxy(note.getBody()).failedAlert));
					break;
				case SkbcommFacade.INSERT_COMPLETE:
					loadList();
					break;	
				case SkbcommFacade.DELETE_COMPLETE:
					loadList();
					break;

				case SkbcommFacade.DELETE_FAILED:
					Alert.show((SkbCommProxy(note.getBody()).sDelFaultMsg));
					break;					
					
					
				case SkbCommProxy.LOAD_STATEMENT_URL_COMPLTE:
					onNotePrint((note.getBody() as SkbCommProxy).url);
					break;
				case SkbCommProxy.LOAD_STATEMENT_URL_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, "("+(note.getBody() as SkbCommProxy).errCode+")"+ (note.getBody() as SkbCommProxy).errText);
					break;
				default:
					break;
			}
		}
		
 		private function onLoadList(proxy:SkbCommProxy):void
 		{
 			view.inrecColl = proxy.inrecList;
 			
 		}
 		
 		private function onLoadBillingList(proxy:SkbCommProxy):void
 		{
 			view.billingList = proxy.billingList;
 			view.billingYearList = proxy.acMedYear;
 			view.clickPopupCall();
 		}
 		
 		private function onLoadComboBillingList(proxy:SkbCommProxy):void
 		{
 			view.PopupBilling.dataProvider = proxy.billingList;
 		}
 		
 		private function onReadList(proxy:SkbCommProxy):void
 		{
 			view.vs.selectedIndex = 1;
 			view.reqView.cbList = proxy.readList; 
 			var vo:SkbVO = new SkbVO();
 			if(view.typeStr == "READ"){
 				vo =  view.dg.selectedItem as SkbVO;
 			}
 			 
 			view.reqView.setLoad(vo as SkbVO, view.typeStr);
 			
			
 		}
 		
 		 
		private function onNotePrint(url:String):void
		{ 
			var req:URLRequest = new URLRequest("../ace_inrec/"+url);
			navigateToURL(req);
		}
 		
        private function get view():SkbCommView
        {
            return viewComponent as SkbCommView;
        }
		
		
	}
}